<!DOCTYPE html>
<html lang="${LANG}" class="dark">

<head>
    <meta charset="UTF-8">
    <title>QBT_TR(New Category)QBT_TR[CONTEXT=TransferListWidget]</title>
    <link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css">
    <script src="scripts/lib/MooTools-Core-1.6.0-compat-compressed.js"></script>
    <script src="scripts/lib/MooTools-More-1.6.0-compat-compressed.js"></script>
    <script src="scripts/localpreferences.js?v=${CACHEID}"></script>
    <script src="scripts/color-scheme.js?v=${CACHEID}"></script>
    <script src="scripts/misc.js?locale=${LANG}&v=${CACHEID}"></script>
    <script src="scripts/pathAutofill.js?v=${CACHEID}"></script>
    <script>
        "use strict";

        window.addEventListener("DOMContentLoaded", () => {
            window.addEventListener("keydown", (event) => {
                switch (event.key) {
                    case "Enter":
                        event.preventDefault();
                        $("categoryNameButton").click();
                        break;
                    case "Escape":
                        event.preventDefault();
                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                        break;
                }
            });

            const searchParams = new URLSearchParams(window.location.search);
            const uriAction = window.qBittorrent.Misc.safeTrim(searchParams.get("action"));
            const uriHashes = window.qBittorrent.Misc.safeTrim(searchParams.get("hashes"));
            const uriCategoryName = window.qBittorrent.Misc.safeTrim(searchParams.get("categoryName"));
            const uriSavePath = window.qBittorrent.Misc.safeTrim(searchParams.get("savePath"));

            if (uriAction === "edit") {
                if (!uriCategoryName)
                    return;

                $("categoryName").disabled = true;
                $("categoryName").value = window.qBittorrent.Misc.escapeHtml(uriCategoryName);
                $("savePath").value = window.qBittorrent.Misc.escapeHtml(uriSavePath);
                $("savePath").focus();
            }
            else if (uriAction === "createSubcategory") {
                $("categoryName").value = window.qBittorrent.Misc.escapeHtml(uriCategoryName);
                $("categoryName").focus();
            }
            else {
                $("categoryName").focus();
            }

            $("categoryNameButton").addEventListener("click", (e) => {
                e.preventDefault();
                e.stopPropagation();

                const savePath = $("savePath").value.trim();
                const categoryName = $("categoryName").value.trim();

                const verifyCategoryName = (name) => {
                    if ((name === null) || (name === ""))
                        return false;
                    if (name.match("^([^\\\\\\/]|[^\\\\\\/]([^\\\\\\/]|\\/(?=[^\\/]))*[^\\\\\\/])$") === null) {
                        alert("QBT_TR(Invalid category name:\nPlease do not use any special characters in the category name.)QBT_TR[CONTEXT=HttpServer]");
                        return false;
                    }
                    return true;
                };

                switch (uriAction) {
                    case "set":
                        if ((uriHashes === "") || !verifyCategoryName(categoryName))
                            return;

                        fetch("api/v2/torrents/createCategory", {
                                method: "POST",
                                body: new URLSearchParams({
                                    category: categoryName,
                                    savePath: savePath
                                })
                            })
                            .then((response) => {
                                if (!response.ok) {
                                    alert(`QBT_TR(Unable to create category)QBT_TR[CONTEXT=Category] ${window.qBittorrent.Misc.escapeHtml(categoryName)}`);
                                    return;
                                }

                                fetch("api/v2/torrents/setCategory", {
                                        method: "POST",
                                        body: new URLSearchParams({
                                            hashes: uriHashes,
                                            category: categoryName
                                        })
                                    })
                                    .then((response) => {
                                        if (!response.ok) {
                                            alert("QBT_TR(Unable to set category)QBT_TR[CONTEXT=Category]");
                                            return;
                                        }

                                        window.parent.updateMainData();
                                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                                    });
                            });
                        break;

                    case "create":
                    case "createSubcategory":
                        if (!verifyCategoryName(categoryName))
                            return;
                        fetch("api/v2/torrents/createCategory", {
                                method: "POST",
                                body: new URLSearchParams({
                                    category: categoryName,
                                    savePath: savePath
                                })
                            })
                            .then((response) => {
                                if (!response.ok) {
                                    alert("QBT_TR(Unable to create category)QBT_TR[CONTEXT=Category]");
                                    return;
                                }

                                window.parent.updateMainData();
                                window.parent.qBittorrent.Client.closeFrameWindow(window);
                            });
                        break;

                    case "edit":
                        fetch("api/v2/torrents/editCategory", {
                                method: "POST",
                                body: new URLSearchParams({
                                    category: uriCategoryName, // category name can't be changed
                                    savePath: savePath
                                })
                            })
                            .then((response) => {
                                if (!response.ok) {
                                    alert("QBT_TR(Unable to edit category)QBT_TR[CONTEXT=Category]");
                                    return;
                                }

                                window.parent.updateMainData();
                                window.parent.qBittorrent.Client.closeFrameWindow(window);
                            });
                        break;
                }
            });
        });

        window.qBittorrent.pathAutofill.attachPathAutofill();
    </script>
</head>

<body>
    <div style="padding: 10px 10px 0px 10px;">
        <label for="categoryName" style="font-weight: bold;">QBT_TR(Category:)QBT_TR[CONTEXT=TransferListWidget]</label>
        <input type="text" id="categoryName" style="width: 99%;">
        <label for="savePath" style="font-weight: bold;">QBT_TR(Save path:)QBT_TR[CONTEXT=TransferListWidget]</label>
        <input type="text" id="savePath" class="pathDirectory" style="width: 99%;">
        <div style="text-align: center; padding-top: 10px;">
            <input type="button" value="QBT_TR(OK)QBT_TR[CONTEXT=Category]" id="categoryNameButton">
        </div>
    </div>
</body>

</html>
